package com.ly.airline.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ly.airline.pojo.Admin;
import com.ly.airline.pojo.User;
import com.ly.airline.service.AdminService;
import com.ly.airline.service.UserService;
import com.ly.airline.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：xxl
 * @ClassName LoginController
 * @date ：Created in 2022/4/25 21:26
 * @description： 登录接口
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    /**
     *@param
     * @return java.lang.String
     * @author xxl
     * @description: 跳转到后台登录界面
     * @date 2022/4/30 0:30
     */
    @GetMapping("mlogin")
    public String toLogin(){
        return "login";
    }

    /**
     *@param
     * @return java.lang.String
     * @author xxl
     * @description: 跳转到前台首页
     * @date 2022/4/30 0:30
     */
    @GetMapping("uIndex")
    public String uIndex(){
        return "qiantai/index";
    }

    /**
     *@param
     * @return java.lang.String
     * @author xxl
     * @description: 登录成功跳转
     * @date 2022/4/30 0:30
     */
    @GetMapping("loginSuccess")
    public String loginSuccess(){
        return "loginSuccess";
    }

    /**
     *@param username
	* @param password
	* @param rand
	* @param request
     * @return com.ly.airline.utils.HttpResult
     * @author xxl
     * @description: 后台登录
     * @date 2022/4/30 0:30
     */
    @PostMapping("login")
    @ResponseBody
    public HttpResult login(String username,String password,String rand,HttpServletRequest request){
        //校验码校验
        String randSession = (String) request.getSession().getAttribute("rand");
        if (rand!=null&&randSession!=null&&!rand.equals(randSession)){
            return HttpResult.success(false,"校验码错误");
        }

        //校验用户名和密码
        //数据库查询用户
        LambdaQueryWrapper<Admin> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Admin::getUsername,username).eq(Admin::getUserpw,password);
        Admin one = adminService.getOne(queryWrapper);
        if (BeanUtil.isEmpty(one)){
            return HttpResult.success(false,"用户名密码错误");
        }
        request.getSession().setAttribute("admin",one);
        return HttpResult.success(true,"登录成功");
    }

    /**
     *@param request
     * @return java.lang.String
     * @author xxl
     * @description: 后台退出登录
     * @date 2022/4/30 0:30
     */
    @GetMapping("adminLogout")
    public String adminLogout(HttpServletRequest request){
        //清除登录用户数据
        request.getSession().setAttribute("admin",null);
        return "login";
    }

    /**
     *@param username
	* @param password
	* @param request
     * @return com.ly.airline.utils.HttpResult
     * @author xxl
     * @description: 前台用户登录
     * @date 2022/4/30 0:30
     */
    @PostMapping("uLogin")
    @ResponseBody
    public HttpResult uLogin(String username,String password,HttpServletRequest request){

        //校验用户名和密码
        //数据库查询用户
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getUserName,username).eq(User::getUserPw,password);
        User one = userService.getOne(queryWrapper);
        if (BeanUtil.isEmpty(one)){
            return HttpResult.success(false,"用户名密码错误");
        }
        //添加用户登录数据
        request.getSession().setAttribute("user",one);
        return HttpResult.success(true,"登录成功");
    }

    /**
     *@param request
     * @return com.ly.airline.utils.HttpResult
     * @author xxl
     * @description: 前台用户退出
     * @date 2022/4/30 0:30
     */
    @GetMapping("userLogout")
    @ResponseBody
    public HttpResult userLogout(HttpServletRequest request){
        //清除用户登录数据
        request.getSession().setAttribute("user",null);
        return HttpResult.success(true,"退出成功");
    }

//    @GetMapping("image")
//    public String image(){
//        System.out.println(2222);
//        return "image";
//    }
}
